package com.quicklife.service.impl;

import java.util.List;

import net.sf.json.JSONObject;

import com.quicklife.pojo.ActionLogs;
import com.quicklife.service.inter.FeedService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 安卓--获取动态
 * 
 * 
 * @author HackerD
 * 
 */
public class FeedServiceImpl implements FeedService {

	@Override
	public String getFriendsFeed(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String id = jsonObject.getString("userid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,f.* from (select a.bussid,b.nickname,b.photo uphoto,c.name "
									+ "busname,d.name cpname,e.name actionname,to_char(ac_time,'yyyy-MM-dd HH:mm:ss') ac_time,a.comments,"
									+ "score,a.photo,recording,video from action_logs a  "
									+ "inner join tuser b on a.userid=b.id "
									+ "inner join business c on a.bussid=c.id "
									+ "inner join coupons d on a.couponid=d.id "
									+ "inner join action_type e on a.ac_type=e.id "
									+ "where   a.userid=(select user2 from friendship where user1=?) "
									+ "order by ac_time) f) "
									+ "where rnum>=? and rnum<?",
							new String[] { id, StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getMyFeed(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String id = jsonObject.getString("userid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,f.* from (select a.bussid,b.nickname,b.photo uphoto,c.name "
									+ "busname,d.name cpname,e.name actionname,to_char(ac_time,'yyyy-MM-dd HH:mm:ss') ac_time,a.comments,"
									+ "score,a.photo,recording,video from action_logs a  "
									+ "inner join tuser b on a.userid=b.id "
									+ "inner join business c on a.bussid=c.id "
									+ "inner join coupons d on a.couponid=d.id "
									+ "inner join action_type e on a.ac_type=e.id "
									+ "where   a.userid=? "
									+ "order by ac_time) f) "
									+ "where rnum>=? and rnum<?",
							new String[] { id, StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

}
